<?php
header('Content-type: application/json; charset=utf-8');
	if(isset($_REQUEST['mod'])){
		include_once('db_connect.php');
		Connect::ConnectMysql($db_sever,$db_user,$db_pass,$db_database);
		$mod=cleanup_text($_REQUEST['mod']);
		switch($mod){
            case 'T_mode':
                $arr=array('result'=>true,'msg'=>'Thành công','debug'=>true,'code'=>'#000');
				echo json_encode($arr);
            break;
			case 'T_ping':
				$arr=array('result'=>true,'msg'=>'Thành công','code'=>'#000');
				echo json_encode($arr);
			break;
			case 'T_cmd':
				if(isset($_REQUEST['session']))
				{
					$session=cleanup_text($_REQUEST['session']);
					$sql="SELECT A.`command_id`, A.`command_content`,A.`command_type`  FROM `am_command` A,`am_cache` B WHERE A.`command_status`=1 and A.`teminal_id`=B.`terminal_id` and B.`session`='{$session}'";
					$query=mysql_query($sql);
					if($query!==false && mysql_num_rows($query))
					{
						$listCommand= array();
						while($row =mysql_fetch_array($query))
						{
							$command= new stdClass();
							$command->cmd_id=$row['command_id'];
							$command->cmd_key=$row['command_type'];
							$command->content=$row['command_content'];							
							array_push($listCommand,$command);
						}
						$arr=array('result'=>true,'msg'=>'Thành công','command'=>$listCommand,'code'=>'#000');
						echo json_encode($arr);
					}
					else
					{
						$arr=array('result'=>false,'msg'=>'Không tồn tại command','code'=>'#000');
						echo json_encode($arr);
					}
				}
				else
				{
					$arr=array('result'=>false,'msg'=>'Thiếu thông tin session','code'=>'#004');
					echo json_encode($arr);
				}
			break;			
			case 'T_checkTime':
				$sql='select Date_format(Now(),\'%Y-%m-%d %T\') as timer;';
				$query=mysql_query($sql);
				if($query!==false)
				{
					$row=mysql_fetch_array($query);
					$arr=array('result'=>true,'msg'=>'thành công','time_server'=>$row['timer'],'code'=>'#000');
					echo json_encode($arr);
				}
				else
				{
					$arr=array('result'=>false,'msg'=>'Không thành công','code'=>'#000');
					echo json_encode($arr);
				}
			break;
			case 'T_getUser':
				if(isset($_REQUEST['session']))
				{
					$session=cleanup_text($_REQUEST['session']);
					$terminal_id=checkExistSession($session);
					if($terminal_id!==false)
					{
						updateSession($session);
						$json=getUser($terminal_id);
						if($json!==false)
						{
							echo json_encode($json);
						}
						else
						{
							$arr=array('result'=>false,'msg'=>'Lỗi Khong ton tai user quan ly termianl nay','code'=>'#004');
							echo json_encode($arr);
						}
					}
					else
					{
						$arr=array('result'=>false,'msg'=>'Lỗi không tồn tại session','code'=>'#009');
						echo json_encode($arr);
					}
				}
				else
				{
					$arr=array('result'=>false,'msg'=>'Thiếu thông tin session','code'=>'#004');
					echo json_encode($arr);
				}
			break;
			case 'T_Noonce':
				$fix_code='AM_KEY_';
				$abs_code=Time();
				$rand_code=rand();
				$code=md5($fix_code.$rand_code.$abs_code);			
				$ip=getIP();
				$sql="INSERT INTO `am_noonce`(`noonce_token`,`noonce_ip`) VALUES ('{$code}','{$ip}')";
				$query=mysql_query($sql);
				if($query!==false)
				{
					$id=mysql_insert_id();
					$token= new stdClass();
					$token->token_id=$id;
					$token->token_key=$code;
					$arr=array('result'=>true,'msg'=>'thành công','token'=>$token,'code'=>'#000');
					echo json_encode($arr);
				}
				else
				{
					$arr=array('result'=>false,'msg'=>'Không thành công','code'=>'#000');
					echo json_encode($arr);
				}
			break;
			case 'T_checkexist':
			if(isset($_REQUEST['session']))
			{
				$session=cleanup_text($_REQUEST['session']);
				$terminal_id=checkExistSession($session);
				if($terminal_id!==false)
				{
					updateSession($session);
					$arr=array('result'=>true,'msg'=>'thành công','code'=>'#000');
					echo json_encode($arr);
				}
				else
				{
					$arr=array('result'=>false,'msg'=>'Lỗi không tồn tại session','code'=>'#005');
					echo json_encode($arr);
				}				
			}
			else
			{
				$arr=array('result'=>false,'msg'=>'Thiếu thông tin session','code'=>'#004');
				echo json_encode($arr);
			}
			break;
			case 'T_checkMD5':
			if(isset($_REQUEST['media_id']) && isset($_REQUEST['md5'])){
				$media_id=cleanup_text($_REQUEST['media_id']);
				$md5File=cleanup_text($_REQUEST['md5']);
				if($media_id=='' || $md5File=='')
				{
					$arr=array('result'=>false,'msg'=>'Thông tin không được để trống','code'=>'#004');
					echo json_encode($arr);
					return;
				}
				$sql="SELECT `media_id` FROM `am_media` WHERE `media_id`='{$media_id}' and `media_md5`='{$md5File}' and `media_status`=1";
				$query=mysql_query($sql);
				if($query!==false && mysql_num_rows($query))
				{
					$arr=array('result'=>true,'msg'=>'Mã MD5 chính xác','code'=>'#000');
					echo json_encode($arr);
				}
				else
				{
					$arr=array('result'=>false,'msg'=>'Mã MD5 không đúng','code'=>'#000');
					echo json_encode($arr);
					return;
				}
			}
			else
			{
				$arr=array('result'=>false,'msg'=>'Thiếu thông tin','code'=>'#004');
				echo json_encode($arr);
				return;
			}
			break;
			case 'T_login':
			if(isset($_REQUEST['data'])&& isset($_REQUEST['token']))
			{
				$data=cleanup_text($_REQUEST['data']);
				$token=cleanup_text($_REQUEST['token']);
				if($data=='')
				{
					$arr=array('result'=>false,'msg'=>'Thông tin đăng nhập không được để trống','code'=>'#004');
					echo json_encode($arr);
					return;
				}
				if($token=='')
				{
					$arr=array('result'=>false,'msg'=>'Thông tin token không được để trống','code'=>'#004');
					echo json_encode($arr);
					return;
				}
				echo json_encode(TeminalLoginToken($token,$data));
			}
			else
			{
				$arr=array('result'=>false,'msg'=>'Thiếu thông tin đăng nhập','code'=>'#004');
				echo json_encode($arr);
			}			
			break;
			case 'T_getDefaultMedia':
				if(isset($_REQUEST['session']))
				{
					$session=cleanup_text($_REQUEST['session']);
					$terminal_id=checkExistSession($session);
					if(isset($_REQUEST['begin'])){
						$start=cleanup_text($_REQUEST['begin']);
					}else{
						$start=0;
					}
					if(isset($_REQUEST['quanity'])){
						$quanity=cleanup_text($_REQUEST['quanity']);
					}else{
						$quanity=100;
					}
					if($terminal_id!==false)
					{
						updateSession($session);
						$json=getVideosDefault($terminal_id,$start,$quanity);
						if($json!==false){
							echo json_encode($json);
						}else{
							$arr=array('result'=>false,'msg'=>'Lỗi không tồn tại','code'=>'#000');
							echo json_encode($arr);
						}
					}
					else
					{
						$arr=array('result'=>false,'msg'=>'Lỗi không tồn tại session','code'=>'#009');
						echo json_encode($arr);
					}
				}
				else
				{
					$arr=array('result'=>false,'msg'=>'Thiếu thông tin session','code'=>'#004');
					echo json_encode($arr);
				}
			break;
			case 'T_getschedule':
				if(isset($_REQUEST['session']))
				{
					$session=cleanup_text($_REQUEST['session']);
					$terminal_id=checkExistSession($session);
					if($terminal_id!==false)
					{
						updateSession($session);
						$json=getVideos($terminal_id);
						if($json!==false)
						{
							echo json_encode($json);
						}
						else
						{
							$arr=array('result'=>false,'msg'=>'Lỗi chưa có lịch cho terminal này','code'=>'#004');
							echo json_encode($arr);
						}
					}
					else
					{
						$arr=array('result'=>false,'msg'=>'Lỗi không tồn tại session','code'=>'#009');
						echo json_encode($arr);
					}
				}
				else
				{
					$arr=array('result'=>false,'msg'=>'Thiếu thông tin session','code'=>'#004');
					echo json_encode($arr);
				}
			break;
			case 'T_getvideo':
				if(isset($_REQUEST['session']))
				{
					$session=cleanup_text($_REQUEST['session']);
					$terminal_id=checkExistSession($session);
					if($terminal_id!==false)
					{
						updateSession($session);
						$json=getVideo($terminal_id);
						if($json!==false)
						{
							echo json_encode($json);
						}
						else
						{
							$arr=array('result'=>false,'msg'=>'Lỗi chưa có lịch cho terminal này','code'=>'#004');
							echo json_encode($arr);
						}
					}
					else
					{
						$arr=array('result'=>false,'msg'=>'Lỗi không tồn tại session','code'=>'#009');
						echo json_encode($arr);
					}
				}
				else
				{
					$arr=array('result'=>false,'msg'=>'Thiếu thông tin session','code'=>'#004');
					echo json_encode($arr);
				}
			break;
			case 'T_getinfor':
				if(isset($_REQUEST['session']))
				{
					$session=cleanup_text($_REQUEST['session']);
					$terminal_id=checkExistSession($session);
					if($terminal_id!==false)
					{
						updateSession($session);
						echo getInforTerminal($terminal_id);
					}
					else
					{
						$arr=array('result'=>false,'msg'=>'Lỗi không tồn tại session','code'=>'#005');
						echo json_encode($arr);
					}			
				}
				else
				{
					$arr=array('result'=>false,'msg'=>'Thiếu thông tin session','code'=>'#004');
					echo json_encode($arr);
				}
			break;
			default:
				LastTime(1);
				$arr=array('result'=>false,'msg'=>'không tồn tại thao tác này','code'=>'#004');
				echo json_encode($arr);
			break;
		};
	}
	else
	{
		$arr=array('result'=>false,'msg'=>'Thiếu thông tin','code'=>'#004');
		echo json_encode($arr);
	}
	function updateSession($data)
	{
		$sql="UPDATE `am_cache` SET `Time`=now() WHERE `session`='{$data}'";
		mysql_query($sql);
	}
	function getVideo($data)
	{
		$sql="SELECT `schedules_id`, `user_id`,  `schedules_date_begin`, `schedules_date_end` FROM `am_schedules` WHERE `termiral_id`='{$data}' and `schedules_date_begin`<=Now() and `schedules_date_end`>Now()";
		$query=mysql_query($sql);
		if($query!==false)
		{
			while($row=mysql_fetch_array($query))
			{
				$user_id=$row['user_id'];
				$schedules_id=$row['schedules_id'];
				$date_start=$row['schedules_date_begin'];
				$date_end=$row['schedules_date_end'];
				$sql="SELECT `schedules_details_id`,`plan_id`,`time_play`, `time_end` FROM `am_schedules_details` WHERE `schedules_id`='{$schedules_id}' and `time_play` >=now() and `time_end` <now() ORDER BY `time_play` ASC LIMIT 0 , 1";
				$query=mysql_query($sql);
				
				if($query!==false && mysql_num_rows($query)>0)
				{
					while($row=mysql_fetch_array($query))
					{
						$plan_id=$row['plan_id'];
						$time_play=$row['time_play'];
						$time_end=$row['time_end'];
						$media=getVideoOfPlaylist($plan_id);
						if($media!==false)
						{
							return $arr=array('result'=>true,'msg'=>'Thông tin video thành công','media'=>$media,'user_id'=>$$user_id,'timeStart'=>$time_play,'timeEnd'=>$time_end,'code'=>'#000');
						}
						else
						{
							return $arr=array('result'=>false,'msg'=>'Lỗi không xác định','media'=>null,'code'=>'#005');
						}						
					}
				}
				else
				{
					$sql="SELECT `plan_id` FROM `am_plan` WHERE `user_id`='{$user_id}' and `plan_default`=1 and `plan_status`=1";
					$query=mysql_query($sql);					
					if($query!==false && mysql_num_rows($query)>0)
					{
						while($row=mysql_fetch_array($query))
						{
							$plan_id=$row['plan_id'];
							$media=getVideoOfPlaylist($plan_id);
							if($media!==false)
							{
								return $arr=array('result'=>true,'msg'=>'Thông tin video thành công','media'=>$media,'user_id'=>$user_id,'timeStart'=>$date_start,'timeEnd'=>$date_end,'code'=>'#000');
							}
							else
							{
								return $arr=array('result'=>false,'msg'=>'Lỗi không xác định','media'=>null,'code'=>'#005');
							}	
						}						
					}
				}			
			}
		}
		return false;
	}
	function getVideosDefault($data,$start,$quanity)
	{
		$sql="SELECT COUNT( * ) as num
				FROM (
					SELECT DISTINCT D.detail_plan_id
					FROM  `am_schedules` A,  `am_plan` C, am_plan_details D
					WHERE A.`termiral_id` =  '{$data}'
					AND A.user_id = C.user_id
					AND C.`plan_default` =1
					AND C.`plan_status` =1
					AND D.plan_id = C.plan_id
				) as Cache_Table";
		$num=0;
		$query=mysql_query($sql);
		if($query!==false){
			$row=mysql_fetch_array($query);
			$num=$row['num'];
		}
		$sql="SELECT distinct B.`media_id`,  B.`media_name`, B.`media_url`, B.`media_content`, B.`media_type_id`, DATE_FORMAT(D.`time_play`,'%T') as timePlay, DATE_FORMAT(D.`time_end`,'%T') as timeEnd, D.plan_id,B.user_id  FROM `am_schedules` A, `am_plan` C, `am_media` B, am_plan_details D WHERE A.`termiral_id`='{$data}' and B.media_id=D.media_id and B.media_status=1 and A.user_id=C.user_id and C.`plan_default`=1 and C.`plan_status`=1 and D.plan_id=C.plan_id order by B.`media_id` limit {$start},{$quanity}";
		$query=mysql_query($sql);
		$medias=array();
		$end=0;
		if($query!==false)
		{
			$date=date('Y-m-d');
			$end=mysql_num_rows($query);
			while($row=mysql_fetch_array($query))
			{		
				$media= new stdClass();
				$media->name=$row['media_name'];
				$media->id=$row['media_id'];
				$media->url=$row['media_url'];
				$media->content=$row['media_content'];
				$media->type=$row['media_type_id'];
				$media->timeStart=$date.' '.$row['timePlay'];
				$media->timeEnd= $date.' '.$row['timeEnd'];
                $media->user_id=$row['user_id'];
				$media->planId=$row['plan_id'];
				array_push($medias,$media);
			}
			return $arr=array('result'=>true,'msg'=>'Thông tin video thành công','media'=>$medias,'num'=>$num,'begin'=>$start,'end'=> (string)($start+$end),'code'=>'#000');
		}
		return false;
	}
	function getVideos($data)
	{
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$time=getdate();
		if($time['hours']>22)
		{
			$time['mday']=$time['mday']+1;
		}
		$strTime=$time['year'].'-'.$time['month'].'-'.$time['mday'];
		$sql="SELECT `schedules_id`, `user_id`,  `schedules_date_begin`, `schedules_date_end` FROM `am_schedules` WHERE `termiral_id`='{$data}' and DATE_FORMAT(`schedules_date_begin`,'%Y-%M-%e')<='{$strTime}' and DATE_FORMAT(`schedules_date_end`,'%Y-%M-%e')>'{$strTime}'";
		$query=mysql_query($sql);
		if($query!==false)
		{
			while($row=mysql_fetch_array($query))
			{
				$user_id=$row['user_id'];
				$schedules_id=$row['schedules_id'];
				$date_start=$row['schedules_date_begin'];
				$date_end=$row['schedules_date_end'];
				$sql="SELECT `schedules_details_id`,`plan_id`,`time_play`, `time_end` FROM `am_schedules_details` WHERE `schedules_id`='{$schedules_id}' and DATE_FORMAT(`time_play`,'%Y-%M-%e')='{$strTime}' ORDER BY `time_play`";
				$query=mysql_query($sql);
				
				if($query!==false && mysql_num_rows($query)>0)
				{
					while($row=mysql_fetch_array($query))
					{
						$plan_id=$row['plan_id'];
						$time_play=$row['time_play'];
						$time_end=$row['time_end'];
						$medias=getVideosOfPlaylist($plan_id,$strTime);
						if($media!==false)
						{
							return $arr=array('result'=>true,'msg'=>'Thông tin video thành công','media'=>$medias,'timeStart'=>$time_play,'timeEnd'=>$time_end,'code'=>'#000');
						}
						else
						{
							return $arr=array('result'=>false,'msg'=>'Lỗi không xác định','media'=>null,'code'=>'#005');
						}						
					}
				}	
			}
		}
		return false;
	}
	function getVideosOfPlaylist($plan_id,$date)
	{
		$sql="SELECT B.`media_id`,  B.`media_name`, B.`media_url`, B.`media_content`, B.`media_type_id` , DATE_FORMAT(A.`time_play`,'%T') as timePlay, DATE_FORMAT(A.`time_end`,'%T') as timeEnd,B.user_id FROM `am_plan_details` A , `am_media` B WHERE A.`plan_id`='{$plan_id}' and B.`media_id`=A.`media_id` and B.media_status=1 order by B.`media_id`";
		$query=mysql_query($sql);
		$medias=array();
		if($query!==false && mysql_num_rows($query)>0)
		{
			while($row=mysql_fetch_array($query))
			{		
				$media= new stdClass();
				$media->name=$row['media_name'];
				$media->id=$row['media_id'];
				$media->url=$row['media_url'];
				$media->content=$row['media_content'];
				$media->type=$row['media_type_id'];
				$media->timeStart=$date.' '.$row['timePlay'];
				$media->timeEnd= $date.' '.$row['timeEnd'];
                $media->user_id=$row['user_id'];
				$media->planId=$plan_id;
				array_push($medias,$media);
			}
			return $medias;
		}
		else
		{
			return false;
		}
	}
	function getVideoOfPlaylist($plan_id)
	{
		$sql="SELECT B.`media_id`,  B.`media_name`, B.`media_url`, B.`media_content`, B.`media_type_id` , DATE_FORMAT(A.`time_play`,'%T') as timePlay, DATE_FORMAT(A.`time_end`,'%T') as timeEnd FROM `am_plan_details` A , `am_media` B WHERE A.`plan_id`='{$plan_id}' and B.`media_id`=A.`media_id` and DATE_FORMAT(NOW(),'%T')>=DATE_FORMAT(A.`time_play`,'%T') and DATE_FORMAT(NOW(),'%T')<DATE_FORMAT(A.`time_end`,'%T') and B.media_status=1";
		$query=mysql_query($sql);
		if($query!==false && mysql_num_rows($query)>0)
		{
			while($row=mysql_fetch_array($query))
			{
				$date=date('Y-m-d');				
				$media= new stdClass();
				$media->name=$row['media_name'];
				$media->id=$row['media_id'];
				$media->url=$row['media_url'];
				$media->content=$row['media_content'];
				$media->type=$row['media_type_id'];
				$media->timeStart=$date.' '.$row['timePlay'];
				$media->timeEnd= $date.' '.$row['timeEnd'];
				$media->planId=$plan_id;
				return $media;
									
			}
		}
		else
		{
			return false;
		}
	}
	function getInforTerminal($data)
	{
		$ip=getIP();
		$sql="SELECT `termiral_name`, `termiral_ip`, `termiral_content`, `termiral_status` FROM `am_termiral` WHERE `termiral_id`='{$data}' and termiral_status=1";
		$query=mysql_query($sql);
		if($query!==false)
		{
			while($row=mysql_fetch_array($query))
			{
				$terminal= new stdClass();
				$terminal->id=$data;
				$terminal->name=$row['termiral_name'];
				$terminal->ip=$row['termiral_ip'];
				$terminal->content=$row['termiral_content'];
				$terminal->status=$row['termiral_status'];
				if($terminal->ip==$ip)
				{
					$terminal->fakeIp=false;
				}
				else
				{
					$terminal->fakeIp=true;
				}
				$arr=array('result'=>true,'msg'=>'Lấy thông tin thành công','terminal'=>$terminal,'code'=>'#000');
				return json_encode($arr);
			}
		}
		else
		{
			$arr=array('result'=>false,'msg'=>'Thiết bị không tồn tại trên hệ thống','code'=>'#005');
			return json_encode($arr);
		}
	}
	function writeCache($data){
		$sql="INSERT INTO `am_cache`(`terminal_id`, `session`) VALUES ('$data',MD5(Concat(Now(),'Altamedia')))";
		$query =mysql_query($sql);
		$id=mysql_insert_id();
		$sql="SELECT  `session` FROM `am_cache` WHERE `cache_id`='{$id}'";
		$query2=mysql_query($sql);
		if(mysql_num_rows($query2)>0)
		{
			$row=mysql_fetch_array($query2);
			return $row['session'];
		}
		return false;
	}
	function checkExistSession($data)
	{
		$sql="SELECT `terminal_id` FROM `am_cache` WHERE `session`='{$data}'";
		$query=mysql_query($sql);
		if($query!==false && mysql_num_rows($query)>0)
		{
			$row=mysql_fetch_array($query);
			return $row['terminal_id'];
		}
		return false;
	}
	function getIP()
	{
		if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
			$ip = $_SERVER['HTTP_CLIENT_IP'];
		} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		} else {
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}
	function LastTime($data){
		$sql="UPDATE `am_cache` SET `Time`=now() WHERE `terminal_id`='$data'";
		return mysql_query($sql);
	}
	
	function deleteCace($data){
		$sql="DELETE FROM `am_cache` WHERE `terminal_id`=$data";
		return mysql_query($sql);
	}
	function cleanup_text($data)
	{	
		$data= stripslashes($data);    
		$data= mysql_real_escape_string($data);
		return trim($data);
	}
	function TeminalLoginToken($token,$data){
		$sql="SELECT `noonce_token` FROM `am_noonce` WHERE `noonce_id`='{$token}'";
		$query=mysql_query($sql);
		if($query!==false && mysql_num_rows($query)>0)
		{
			$row=mysql_fetch_array($query);
			$noonce=$row['noonce_token'];
			$sql_token="DELETE FROM `am_noonce` WHERE `noonce_id`={$token}";		
			mysql_query($sql_token);
			return TerminalLogin($data,$noonce);			
		}
		else
		{
			return array('result'=>false,'msg'=>'Không tồn tại token','code'=>'#007');
		}
	}	
	function TerminalLogin($data, $noonce)
	{
		$sql="SELECT A.`termiral_id` FROM `am_termiral` A where MD5(CONCAT(A.`termiral_ip`,'{$noonce}',A.`termiral_pass`))
		='$data' and A.termiral_status=1";
		$result=mysql_query($sql);
		if($result === false)
		{			
			return array('result'=>false,'msg'=>'Thông tin đăng nhập không đúng, hoặc không tồn tại','code'=>'#005');
		}
		if(mysql_num_rows($result)>0)
		{
			while($row=mysql_fetch_array($result))
			{
				$terminal_id=$row['termiral_id'];
				$session=writeCache($terminal_id);
				$sql="SELECT A.`user_id`, A.schedules_date_begin, A.schedules_date_end FROM `am_schedules` A where a.termiral_id='{$terminal_id}' and A.schedules_date_begin<=now() and A.schedules_date_end>now()";
				$result=mysql_query($sql);
				if($result!==false && mysql_num_rows($result)>0){
					while($row=mysql_fetch_array($result)){
						$user = new stdClass();
						$user->user_id=$row['user_id'];
						$user->user_name='';
						$user->user_full_name='';
						$user->timeStart=$row['schedules_date_begin'];
						$user->timeEnd=$row['schedules_date_end'];						
						return array('result'=>true,'msg'=>'đăng nhập thành công','session'=>$session,'user'=>$user,'code'=>'#000');
					}
				}else{
					return array('result'=>true,'msg'=>'đăng nhập thành công','session'=>$session,'user'=>null,'code'=>'#000');
				}
			}
		}
		else
		{
			return array('result'=>false,'msg'=>'Thông tin đăng nhập không đúng, hoặc không tồn tại','code'=>'#000');
		}
	}
	function getUser($data){
		$sql="SELECT A.`user_id`, A.schedules_date_begin, A.schedules_date_end FROM `am_schedules` A where a.termiral_id='{$data}' and A.schedules_date_begin<=now() and A.schedules_date_end>now()";
		$result=mysql_query($sql);
		if($result!==false && mysql_num_rows($result)>0){
			while($row=mysql_fetch_array($result)){
				$user = new stdClass();
				$user->user_id=$row['user_id'];
				$user->user_name='';
				$user->user_full_name='';
				$user->timeStart=$row['schedules_date_begin'];
				$user->timeEnd=$row['schedules_date_end'];						
				return array('result'=>true,'msg'=>'thanh cong','user'=>$user,'code'=>'#000');
			}
		}else{
			return array('result'=>false,'msg'=>'khong thanh cong','code'=>'#000');
		}
	}
	
	
?>